import router from './router'
import store from './store'

router.beforeEach(async(to, from, next) => {
  const token = store.state.user.token
  const path = to.path
  const whiteList = ['/login', '/404', '/register']

  if (token && path === '/login') {
    next('/')
  }

  if (token && path !== '/login') {
    if (!store.state.user.userInfo) {
      await store.dispatch('user/getInfo')
    }
    next()
  }

  if (!token && whiteList.includes(path)) {
    next()
  }

  if (!token && !whiteList.includes(path)) {
    next('/login')
  }
})
